package com.xshang.tenant;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.sql.DataSource;

/**
 * 缺省租户数据源服务
 */
@Slf4j
public abstract class DefaultTenantDataSourceService implements TenantDataSourceService {
    /**
     * 注入多租户数据源对象
     */
    @Resource
    private TenantDataSource tenantDataSource;

    @Override
    public void refresh() {
        log.info("不用做什么,实现一下就好了");
    }

    @Override
    public void add(String dataSourceName, DataSource dataSource) {
        tenantDataSource.add(dataSourceName, dataSource);
    }

    @Override
    public void update(String dataSourceName, DataSource dataSource) {
        tenantDataSource.update(dataSourceName, dataSource);
    }

    @Override
    public void remove(String dataSourceName) {
        tenantDataSource.remove(dataSourceName);
    }

    @Override
    public String getTenantDataSourceBeanName(String tenantId) {
        // 默认使用tenantId作为数据源
        return StringUtils.hasText(tenantId) ? tenantId.trim() : "";
    }
}
